package com.ckf.login_wx.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Repertory;
import com.ckf.login_wx.entity.RepertoryNum;
import com.ckf.login_wx.entity.RepertorySum;
import com.ckf.login_wx.entity.RepertoryXH;
import com.ckf.login_wx.entity.Sell;
import com.ckf.login_wx.servic.RepertoryNumService;
import com.ckf.login_wx.servic.RepertoryService;
import com.ckf.login_wx.servic.RepertoryXHService;
import com.ckf.login_wx.servic.SellService;
import com.ckf.login_wx.util.Idname;
import com.github.pagehelper.PageHelper;

@RestController

public class Repertorycontroller {
	@Autowired
    private RepertoryService repertoryService;
	@Autowired
    private RepertoryXHService repertoryXHService;
	@Autowired
    private RepertoryNumService repertoryNumService;
	@Autowired
    private SellService sellService;
	@PostMapping("/addstockxh")
	public Object addstockXH(int id,String modele,String weight,String colour,Integer wall,String faf){
		if(repertoryXHService.list(new QueryWrapper<RepertoryXH>().eq("modele", modele)).size()!=0&&faf.equals("add")){
			return "该型号规则已存在!";
		}
	    String stu="";
	    RepertoryXH RepertoryXH=new RepertoryXH();
	    RepertoryXH.setModele(modele);
	    RepertoryXH.setWeight(weight);
	    RepertoryXH.setColour(colour);
	    RepertoryXH.setWall(wall);
		if(faf.equals("add")){
	        if (repertoryXHService.save(RepertoryXH)){
	        	stu="添加成功";
	        }else {
	        	stu="添加失败";
	        }
		}
		if(faf.equals("up")){
			RepertoryXH.setId(id);
	        if (repertoryXHService.updateById(RepertoryXH)){
	        	stu="修改成功";
	        }else {
	        	stu="修改失败";
	        }
		}
	    return stu;
	}
    @PostMapping("/addordstock")
    public Map<String, Object> addordstock(String modele,String weight,String colour,int wall,String time,int quantite,String saisie) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Repertory Repertory=new Repertory();
        Repertory.setModele(modele);
        Repertory.setWeight(weight);
        Repertory.setColour(colour);
        Repertory.setWall(wall);
        Repertory.setQuantite(quantite);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Repertory.setTime(simpleDateFormat.parse(time));
        Repertory.setSaisie(saisie);
		if (repertoryService.save(Repertory)){
			if(repertoryNumService.list(new QueryWrapper<RepertoryNum>().eq("modele", modele)).size()==0){
				RepertoryNum RepertoryNum=new RepertoryNum();
				RepertoryNum.entity(Repertory);
				repertoryNumService.save(RepertoryNum);
			}else{
				RepertoryNum RepertoryNum=repertoryNumService.list(new QueryWrapper<RepertoryNum>().eq("modele", modele)).get(0);
				RepertoryNum.setQuantite(RepertoryNum.getQuantite()+quantite);
				RepertoryNum.setNum(RepertoryNum.getNum()+quantite);
				repertoryNumService.updateById(RepertoryNum);
			}
            map.put("code",200);
            map.put("result","添加成功");
            System.out.println("添加成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }

    @PostMapping("/upstockbyid")
    public Object upstockbyid(String id,String modele,String weight,String colour,int wall,String time,int quantite,String saisie) throws ParseException{
    	Map<String, Object> map = new HashMap<>();
        Integer i = Integer.parseInt(id);
        Repertory Repertory=new Repertory();
        Repertory.setId(i);
        Repertory.setModele(modele);
        Repertory.setWeight(weight);
        Repertory.setColour(colour);
        Repertory.setWall(wall);
        Repertory.setQuantite(quantite);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Repertory.setTime(simpleDateFormat.parse(time));
        Repertory.setSaisie(saisie);
		if (repertoryService.updateById(Repertory)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    //最初统计搜索
    @GetMapping("/selestockbysumsearch")
    public Object selestockbysumsearch(String search,String talk){

//        QueryWrapper<Repertory> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
    	
    	
    	List<Repertory> list=repertoryService.selectlike(Idname.getname(search),talk);
    	List<RepertorySum> listsum = new ArrayList<RepertorySum>();
    	int sum=0,sum2=0;String s="";
    	for(int n=0;n<list.size();n++){
    		sum=list.get(n).getQuantite();s=list.get(n).getModele();

//        	QueryWrapper<Sell> queryWrapper2 = new QueryWrapper<>();
//            Map<String, Object> mapuser2 = new HashMap<>();
//    		mapuser2.put("modele",list.get(n).getModele());
//            queryWrapper2.allEq(mapuser2);
//        	List<Sell> sell =sellService.list(queryWrapper2);
//        	for(int i=0;i<sell.size();i++){
//        		sum2=sum2+sell.get(i).getQuantite();
//        	}
    		
    		for(int j=0;j<list.size();j++){
    			if(n==j)continue;
    		if(list.get(j).getModele().equals(s)){
    			sum=sum+list.get(j).getQuantite();
    		}
    		}
    		RepertorySum a=new RepertorySum(list.get(n));
    		a.setQuantite(sum);a.setQuantites(sum-sum2);a.setQuantiteex(1);
    		listsum.add(a);
    	}
    	for(int m=0;m<listsum.size();m++){
    		s=listsum.get(m).getModele();
    		for(int q=0;q<listsum.size();q++){
    			if(m==q)continue;
        		if(listsum.get(q).getModele().equals(s)){
        			listsum.remove(q);
        		}
        	}
    	}
    	
    	for(int m2=0;m2<listsum.size();m2++){
    		sum2=0;
    		QueryWrapper<Sell> queryWrapper2 = new QueryWrapper<>();
            Map<String, Object> mapuser2 = new HashMap<>();
            mapuser2.put("modele",listsum.get(m2).getModele());
            queryWrapper2.allEq(mapuser2);
        	List<Sell> sell =sellService.list(queryWrapper2);
        	for(int i=0;i<sell.size();i++){
        		sum2=sum2+sell.get(i).getQuantite();
        	}
        	listsum.get(m2).setQuantites(listsum.get(m2).getQuantite()-sum2);
    	}
    	
        return listsum;
    }
    //最初统计，没有分页
    @GetMapping("/selestockbysum")
    public Object selestockbysum(){
    	
    	
    	List<Repertory> list=repertoryService.list();
    	List<RepertorySum> listsum = new ArrayList<RepertorySum>();
    	int sum=0,sum2=0;String s="";
    	for(int n=0;n<list.size();n++){
    		sum=list.get(n).getQuantite();s=list.get(n).getModele();
    		

    		for(int j=0;j<list.size();j++){
    			if(n==j)continue;
    		if(list.get(j).getModele().equals(s)){
    			sum=sum+list.get(j).getQuantite();
    		}
    		}
    		RepertorySum a=new RepertorySum(list.get(n));
    		a.setQuantite(sum);a.setQuantites(sum-sum2);a.setQuantiteex(1);
    		listsum.add(a);
    	}
    	for(int m=0;m<listsum.size();m++){
    		s=listsum.get(m).getModele();
    		for(int q=0;q<listsum.size();q++){
    			if(m==q)continue;
        		if(listsum.get(q).getModele().equals(s)){
        			listsum.remove(q);
        		}
        	}
    	}
    	
    	for(int m2=0;m2<listsum.size();m2++){
    		sum2=0;
    		QueryWrapper<Sell> queryWrapper = new QueryWrapper<>();
            Map<String, Object> mapuser = new HashMap<>();
            mapuser.put("modele",listsum.get(m2).getModele());
            queryWrapper.allEq(mapuser);
        	List<Sell> sell =sellService.list(queryWrapper);
        	for(int i=0;i<sell.size();i++){
        		sum2=sum2+sell.get(i).getQuantite();
        	}
        	System.out.println(sum2+"********************************************************************"+listsum.get(m2).getModele());
        	listsum.get(m2).setQuantites(listsum.get(m2).getQuantite()-sum2);
    	}
    	
    	
        return listsum;
    }
    //最初统计，没有分页
    @GetMapping("/selestockallbysum")
    public Object selestockallbysum(){
        return repertoryService.list();
    }
    @GetMapping("/selestock")
    public Object selestock(String pagenew){
        return repertoryService.list();
    }
    @GetMapping("/selestockxh")
    public Object selestockxh(){
        return repertoryXHService.list();
    }
    @RequestMapping("/selestockall")
    public Object selestockall(){
        return repertoryService.list();
    }
    @GetMapping("/selestockbyid")
    public Object selestockbyid(String id){
    	Integer idint = Integer.parseInt(id);
        return repertoryService.getById(idint);
    }
    @GetMapping("/selestockxhbyid")
    public Object selestockxhbyid(String id){
    	Integer idint = Integer.parseInt(id);
        return repertoryXHService.getById(idint);
    }
    @GetMapping("/stock/search")
    public Object stocksearch(String talk,String search){
        return repertoryService.list(new QueryWrapper<Repertory>().like(Idname.getname(search), talk));
        
    }
    @GetMapping("/stock/search/tj")
    public Object stocksearchbytj(String talk,String search,String username){
    	Map<String, Object> map = new HashMap<>();
    	List<Repertory> list =new ArrayList<Repertory>();
	    	Calendar calendar = Calendar.getInstance();
	        calendar.setTime(new Date());
    	if(search.equals("当天")){
	        calendar.add(Calendar.DATE,1);
	      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	      String timetop=simpleDateFormat.format(new Date());
	      String timeend=simpleDateFormat.format(calendar.getTime());
            list =repertoryService.list(new QueryWrapper<Repertory>().like("modele", talk).eq("saisie", username).ge("time", timetop).lt("time", timeend));
    	}
    	if(search.equals("当前月")){
	        calendar.add(Calendar.MONTH,1);
	      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
	      String timetop=simpleDateFormat.format(new Date());
	      String timeend=simpleDateFormat.format(calendar.getTime());
            list =repertoryService.list(new QueryWrapper<Repertory>().like("modele", talk).eq("saisie", username).ge("time", timetop+"-01 00:00:00").lt("time", timeend+"-01 00:00:00"));
    	}
    	if(search.equals("全部")){
    		list =repertoryService.list(new QueryWrapper<Repertory>().like("modele", talk).eq("saisie", username));
    	}
    	
        double money=0.00;int num=0;
    	for (int i = 0; i < list.size(); i++) {
			money+=list.get(i).getMoney();
			num+=list.get(i).getQuantite();
		}
    	map.put("list", list);
    	map.put("money", money);
    	map.put("num", num);
    	return map;
        
    }
	@GetMapping("/stocknum/search")
    public Object stocknumsearch(String talk,String search,String username){

    	Map<String, Object> map = new HashMap<>();
    	List<RepertoryNum> a= repertoryNumService.list(new QueryWrapper<RepertoryNum>().like(Idname.getname(search), talk));
    	int num=0,quantite=0;
    	for (int j = 0; j < a.size(); j++) {
			num+=a.get(j).getNum();
			quantite+=a.get(j).getQuantite();
		}
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
        map.put("data",a);
        map.put("num",num);
        map.put("quantite",quantite);
        return map;
    }
    @GetMapping("/delestockbyid")
    public Object delestockbyid(Integer id,String pagenew){
        return repertoryService.removeById(id);
    }
    @GetMapping("/delestockxhbyid")
    public Object delestockxhbyid(Integer id){
    	return repertoryXHService.removeById(id);
    }
    @RequestMapping("/selemodeall")
    public Object selemodeall(){
    	repertoryService.list();
    	List<String> s =new ArrayList<String>();
    	for(int i=0;i<repertoryService.list().size();i++){
    		s.add(repertoryService.list().get(i).getModele());
    	}
    	HashSet<String> set=new LinkedHashSet<>(s);
    	for(String word:s){
    	    set.add(word);
    	}
        return set;
    }
	//查询所有型号
	@RequestMapping("/seleallstockbymodele")
    public Object seleallstockbymodele(){
		Map<String, Object> map = new HashMap<>();
        List<RepertoryNum> a= repertoryNumService.list();
		List<String> modele =new ArrayList<String>();
		List<Integer> num =new ArrayList<Integer>();
    	for (int j = 0; j < a.size(); j++) {
    		modele.add(a.get(j).getModele());
    		num.add(a.get(j).getNum());
		}

        map.put("count",modele.size());
        map.put("data",modele);
        map.put("num",num);
        return map;
    }
	//查询所有型号规范
	@RequestMapping("/seleallstockxhbymodele")
    public Object seleallstockXHbymodele(){
		Map<String, Object> map = new HashMap<>();
        List<RepertoryXH> a= repertoryXHService.list();
		List<String> modele =new ArrayList<String>();
		List<String> weight =new ArrayList<String>();
		List<Integer> wall =new ArrayList<Integer>();
		List<String> colour =new ArrayList<String>();
    	for (int j = 0; j < a.size(); j++) {
    		modele.add(a.get(j).getModele());
    		wall.add(a.get(j).getWall());
    		colour.add(a.get(j).getColour());
    		weight.add(a.get(j).getWeight());
		}

        map.put("count",modele.size());
        map.put("data",modele);
        map.put("weight",weight);
        map.put("wall",wall);
        map.put("colour",colour);
        return map;
    }
	@RequestMapping("/selestockbymodele")
    public Object selestockbymodele(String modele){
        return repertoryXHService.list(new QueryWrapper<RepertoryXH>().eq("modele", modele)).get(0);
    }
	@RequestMapping("/selestocknum")
    public Object selestocknum(){
    	Map<String, Object> map = new HashMap<>();
    	List<RepertoryNum> a= repertoryNumService.list();
    	int num=0,quantite=0;
    	for (int j = 0; j < a.size(); j++) {
			num+=a.get(j).getNum();
			quantite+=a.get(j).getQuantite();
		}
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
        map.put("data",repertoryNumService.list());
        map.put("num",num);
        map.put("quantite",quantite);
        return map;
    }
	@RequestMapping("/selestocknumsearch")
    public Object selestockNUMsearch(String talk,String search){
        return repertoryNumService.list(new QueryWrapper<RepertoryNum>().like(Idname.getname(search), talk));
    }
}
